package com.dongdongshop.mq;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.pojo.salepoints.SalePoints;
import com.dongdongshop.service.salepoints.SalePointsService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component
//@RocketMQMessageListener(topic = "sale",consumerGroup = "Sale-consumer")//配置消费者信息
@RocketMQMessageListener(consumerGroup = "my-order",topic = "countAndOrder")
public class Pointsconsumer implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {

    @Reference
    private SalePointsService salePointsService;
    @Override
    public void onMessage(String s) {
        System.out.println("消息消费成功"+s);
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                MessageExt message = list.get(0);//消息在list中
                try {
                    String messageBody = new String(message.getBody(), "utf-8");
                   // String[] split = messageBody.split(",");
                    List<String> split= JSONObject.parseArray(messageBody,String.class);//转成list
                    //根据id查询 存在修改 不存在增加
                    String userId = split.get(1);
                    BigDecimal price = new BigDecimal(split.get(2));
                    Double db = Double.valueOf(split.get(2)).intValue() * 0.1;
                    int poit = new Double(db).intValue();;
                    SalePoints salePoints = salePointsService.selectByUserId(userId);
                    //如果存在
                    if(salePoints != null){
                        //设置消费总金额
                        salePoints.setSalePrice(salePoints.getSalePrice().add(price));
                        //设置积分
                        salePoints.setSalePoit(salePoints.getSalePoit()+poit);
                        //修改积分
                        salePointsService.updateById(salePoints);
                    }else {//如果不存在
                        SalePoints addSale = new SalePoints();
                        addSale.setUserName(split.get(0));
                        addSale.setUserId(split.get(1));
                        addSale.setSalePrice(price);
                        addSale.setSalePoit(poit);
                        addSale.setSaleGrade("青铜");
                        //添加积分
                        salePointsService.insertSale(addSale);
                    }
                    System.out.println("ACK消息消费成功"+messageBody);
                } catch (Exception e) {
//                     e.printStackTrace();
                    //报错处理(记录日志，错误信息)
                    System.out.println("消息消费失败，稍后再试");
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
    }
}
